package com.lagou.edu.service.impl;

import com.lagou.edu.dao.CodeDao;
import com.lagou.edu.pojo.Code;
import com.lagou.edu.service.CreateCodeService;
import com.lagou.edu.util.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CreateCodeServiceImpl implements CreateCodeService {
    @Autowired
    private CodeDao codeDao;

    @Override
    public String createCode(String email) {
        // 生成4位验证码
        String codeStr = CodeUtil.createCode(4);

        Date now = new Date();

        Code code = new Code();
        code.setEmail(email);
        code.setCode(codeStr);
        code.setCreatetime(now);
        code.setExpiretime(new Date(now.getTime() + 300000));
        codeDao.save(code);

        return codeStr;
    }
}
